# 夜花夺还作战
import log_proc as log
from basic_operations import BasicOperations
from constants import *
import time

class RetrieveFireworks(BasicOperations):
    def __init__(self, teamNumber):
        self.teamNo = teamNumber

    # 图中执行前进一次
    def normalBattleOnce(self):
        log.info("- 开始一个节点")
        time.sleep(1)

        # 1.可能有选阵型
        # 2.一定有，等战斗结果,多次点击
        topLoc1 = []
        topLoc2 = []
        # topLoc6 = []
        topLoc7 = []
        attempt = 0
        while attempt < 3 * ATTLIMIT:
            targetPath = self.autoScreemShot()
            topLoc1, botLoc1 = self.checkLocationOnly(targetPath, NAME_FORMAT + DEFAULT_FORMAT)  # 选阵型
            topLoc2, botLoc2 = self.checkLocationOnly(targetPath, NAME_BATTLERESULT)  # 战斗结果
            # topLoc6, botLoc6 = self.checkLocationOnly(targetPath, NAME_HOST)  # 没走完给定步数，进入末尾资源点，结束重置到本丸界面
            topLoc7, botLoc7 = self.checkLocationOnly(targetPath, NAME_CONTBATTLE)  # 下一步 的图标检测

            if len(topLoc1) == 0 and len(topLoc2) == 0 and len(topLoc7) == 0:
                self.inputLocationAndClick(DEFAULT_TOPLOC, DEFAULT_BOTLOC)  # 如果出现减肥，点击跳过
                time.sleep(3)
                attempt = attempt + 1
            else:
                break

        if len(topLoc7) != 0:  # 直接下一步
            log.info("没有检测到 战斗结果，直接下一步")
        elif len(topLoc2) != 0:  # 战斗结果
            # 点击,跳过掉刀，进入3，4
            clickNum = 2
            while clickNum > 0:
                self.inputLocationAndClick(DEFAULT_TOPLOC, DEFAULT_BOTLOC)
                clickNum = clickNum - 1
        elif len(topLoc1) != 0:  # 选阵型
            self.inputLocationAndClick(topLoc1, botLoc1)

            if not self.toNextClick(NAME_FORMAT_CONF):
                log.info("未识别到 阵型确认")
                return False

            # 检测战斗结果, 点击，进入3，4
            if not self.toNextClick(NAME_BATTLERESULT, True, 3, 2 * ATTLIMIT):
                log.info("阵型选择后，未识别到 战斗结果")
                return False
        # elif len(topLoc6) != 0:
        #     log.info("进入末尾，提前回城")
        #     return False
        else:
            log.info("未识别到下一步，" + NAME_FORMAT + DEFAULT_FORMAT + " 或者 " + NAME_BATTLERESULT)
            return False

        # 3.可能有掉刀，在战斗结果画面左侧反复点击可进入下一步
        # 4.继续进军 或 过王点回到本丸
        attempt = 0
        while attempt < 2*ATTLIMIT:
            targetPath = self.autoScreemShot()
            topLoc5, botLoc5 = self.checkLocationOnly(targetPath, NAME_CONTBATTLE)
            topLoc6, botLoc6 = self.checkLocationOnly(targetPath, NAME_HOST)  # 走过王点或没走完给定步数，进入资源点，结束重置到本丸界面

            if len(topLoc5) == 0 and len(topLoc6) == 0:
                self.inputLocationAndClick(DEFAULT_TOPLOC, DEFAULT_BOTLOC)  # 如果掉刀，点击跳过
                time.sleep(3)
                attempt = attempt + 1
            else:
                break

        if len(topLoc5) != 0:
            self.inputLocationAndClick(topLoc5, botLoc5)
            return True
        elif len(topLoc6) != 0:
            log.info("进入末尾，提前回城")
            return False
        else:
            log.info("未检测到 下一步 或 本丸")
            return False

    # 一次 夺还
    def retrieveOnce(self):
        # 0. 本丸初始画面检测
        maxLoc, bottomRight = self.checkLocation(NAME_HOST)
        if len(maxLoc) == 0:
            log.info("未找到本丸")
            return False
        self.inputLocationAndClick(DEFAULT_TOPLOC, DEFAULT_BOTLOC)

        # 1. 点击侧栏出阵
        maxLoc, bottomRight = self.checkLocation(NAME_ENTERFILED)
        if len(maxLoc) == 0:
            log.info("未找到侧栏出阵")
            return False
        self.inputLocationAndClick(maxLoc, bottomRight)

        # 2. 点击 大出阵 图标
        maxLoc, bottomRight = self.checkLocation(NAME_NORMALENTERFILED)
        if len(maxLoc) == 0:
            log.info("未找到出阵界面出阵图标")
            return False
        self.inputLocationAndClick(maxLoc, bottomRight)

        # 3. 点击 活动地图
        maxLoc, bottomRight = self.checkLocation(NAME_RETRIFIRE)
        if len(maxLoc) == 0:
            log.info("未找到 夜花夺还活动 图标")
            return False
        self.inputLocationAndClick(maxLoc, bottomRight)

        # 4. 点击 部队选择 图标
        maxLoc, bottomRight = self.checkLocation(NAME_CHOOSETEAM)
        if len(maxLoc) == 0:
            log.info("未找到 部队选择 图标")
            return False
        self.inputLocationAndClick(maxLoc, bottomRight)

        # 5. 选择 指定队伍
        # if not self.toNextClick(NAME_TEAM + self.teamNo):  # 模式匹配出错了，需要修改
        #     log.info("未找到 队伍"+str(self.teamNo)+" 图标")
        #     return False

        # 6. 点击 即刻出阵
        if not self.toNextClick(NAME_STARTBATTLE):
            log.info("未找到 即刻出阵 图标")
            return False

        # 7. 点击 是否使用花火石 确认窗口
        if not self.toNextClick(NAME_RETRIFIRE_YES):
            log.info("未找到 即刻出阵 图标")
            return False

        # 8. 图中战斗
        while True:
            if not self.normalBattleOnce():
                log.info("继续前进结束")
                break

        return True

    # 重伤检测
    def seriousInjuryDetection(self):
        pass